
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import 'babel-polyfill'
// import 'normalize.css/normalize.css' // A modern alternative to CSS resets
import './plugins/flexible'
// import MINT from 'mint-ui'
// console.log(MINT)
import store from './store'
// import FastClick from 'fastclick'
import './filters'
import '@/utils/directives'
// import VCconsole from 'vconsole'
// icon
require('@/assets/common/fonts/iconfont.css')
// if ('addEventListener' in document) {
//   document.addEventListener(
//     'DOMContentLoaded',
//     function() {
//       FastClick.attach(document.body)
//     },
//     false
//   )
// }
// Vue.use(VCconsole)
// Vue.use(MINT)
import { getScrollTop, setScrollTop } from '@/utils'
const routerList = []
router.beforeEach((to, from, next) => {
  // 返回上一级浏览位置,以及路径
  const position = getScrollTop()
  const currentRouterIndex = routerList.findIndex(e => {
    return e.path === from.fullPath
  })

  if (currentRouterIndex !== -1) {
    routerList[currentRouterIndex].position = position
  } else {
    routerList.push({
      path: from.fullPath,
      position: position
    })
  }
  // console.log(routerList)
  //   next()
  // window.localStorage.setItem(
  //   'share_token',
  //   '6f0a90b78b818a2824024a3fcfcbc42b'
  // )
  const token = window.localStorage.getItem('share_token')
  if (token) {
    if (to.path === '/author') {
      next({
        path: '/'
      })
    } else {
      // next()
      store
        .dispatch('GetUserInfo', {
          user_token: token
        })
        .then(res => {
          // 拉取用户信息
          next()
        })
    }
  } else {
    if (to.path !== '/author') {
      // next({
      //   path: '/author',
      //   query: { token: 1234556, msg: 201 } // 将跳转的路由path作为参数，登录成功后跳转到该路由
      // })
      // 保存用户进入的url
      if (to.path === '/shop' || to.path === '/product') {
        window.sessionStorage.setItem('authUrl', to.fullPath) // 保存用户进入的url
      }
      store.dispatch('GetAuthUrl').then(res => {
        // 跳转
        window.location.href = res.data.url
      })
    } else {
      next()
    }
  }
})
router.afterEach((to, from, next) => {
  // 解决 keekAlive导致列表页不刷新
  const savedPosition = routerList.find(e => {
    return e.path === to.fullPath
  })
  if (typeof savedPosition !== 'undefined') {
    Vue.nextTick(() => {
      setScrollTop(savedPosition.position)
    })
  } else {
    Vue.nextTick(() => {
      setScrollTop(0)
    })
  }
})
Vue.config.productionTip = false

/* eslint-disable no-new */
Vue.prototype.errorEvent = e => {
  e.target.src = './assets/images/logo.png'
}
new Vue({
  el: '#app',
  store,
  router,
  template: '<App/>',
  components: {
    App
  }
})
